package com.fh.pms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fh.pms.entity.ProductCategory;
import com.fh.pms.mapper.ProductCategoryMapper;
import com.fh.pms.service.IProductCategoryService;
import com.fh.result.ResultObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author F
 * @since 2021-05-21
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {




    @Override
    public void updateShowStatus(Long id, Integer showStatus) {
        ProductCategory category = getById(id);
        category.setShowStatus(showStatus);
        updateById(category);
    }

    @Override
    public void updateFactoryStatus(Long id, Integer navStatus) {
        ProductCategory category = getById(id);
        category.setNavStatus(navStatus);
        updateById(category);
    }

    @Override
    public void saveOrUpdateCategoryList(ProductCategory category) {
        if(category.getParentId()==null) {
            category.setParentId(0L);
            if (category.getLevel() == null) {
                category.setLevel(0);
            } else {
                category.setLevel(1);
            }
        }


        super.saveOrUpdate(category);
    }



    public  List<Map<String, Object>> listMap(List<ProductCategory> list ,Long  pid ){
        List<Map<String, Object>>  Maplist= new ArrayList<Map<String, Object>>();
        list.forEach(PP->{
            Map<String, Object>  map = null;
            if (pid.equals(PP.getParentId())){
                map= new HashMap<>();
                map.put("value",PP.getId());
                map.put("label",PP.getName());
                map.put("children",listMap(list,PP.getId()));

            }
            if (map!=null){
                Maplist.add(map);
            }
        });

        return Maplist;
    }

    @Override
    public ResultObject queryAllList() {
        List<ProductCategory> list = list();
        List<Map<String, Object>> pidListAll = getPidListAll(list, 0L);
        return ResultObject.success(pidListAll);
    }
    public List<Map<String,Object>> getPidListAll(List<ProductCategory> productCategoryList,Long pid){
        List<Map<String,Object>> list = new ArrayList<>();
        productCategoryList.forEach(x ->{
            if(x.getParentId()==pid){
                Map<String, Object> map = new HashMap<>();
                map.put("value",x.getId());
                map.put("label",x.getName());
                if(x.getLevel()==0){
                    map.put("children",getPidListAll(productCategoryList,x.getId()));
                }
                list.add(map);
            }
        });
        return list;
    }

}
